package com.chengyu.core.controller.shop;

import com.chengyu.core.component.OperationLog;
import com.chengyu.core.controller.UserBaseController;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.CommonException;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.model.UmsShopCate;
import com.chengyu.core.result.CommonResultCode;
import com.chengyu.core.service.shop.ShopCateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "店铺分类")
@Controller
@RequestMapping("/shop/cate")
public class ShopCateController extends UserBaseController {

	@Autowired
	private ShopCateService shopCateService;



//	@ApiOperation(value = "商品详情")
//	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
//	@ApiImplicitParam(name = "type", value = "商品类型:normal=普通商品,video=视频号商品,seckill=秒杀商品", defaultValue = "normal")
//	public CommonResult<ProductDetailResponse> getDetail(@Validated @PathVariable Integer id,
//														 @RequestParam(value = "type", required = false) String type) {
//		return CommonResult.success(productService.getDetail(id, type));
//	}





	@OperationLog
	@ApiOperation(value = "detail")
	@ResponseBody
	@RequestMapping(value="/detail", method=RequestMethod.GET)
	public CommonResult<UmsShopCate> detail(Integer id) throws ServiceException {

		UmsShopCate  shopCate  =shopCateService.getShopCateById(id);
		return CommonResult.success(shopCate);
	}



	@OperationLog
	@ApiOperation(value = "编辑店铺类目")
	@ResponseBody
	@RequestMapping(value="/submit", method=RequestMethod.POST)
	public CommonResult<String> editSubmit(@RequestBody @Validated  UmsShopCate cate) throws ServiceException {

//		System.out.println("cate.getType();"+cate.getType());
		if (cate.getType() == null || cate.getType().equals(0)){
			cate.setType(1);
		}

//		System.out.println("submitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmit");
//		System.out.println(cate);

		if(cate.getId() == null){
			shopCateService.addShopCate(getCurrentShop(), cate.getPid(), cate.getName(), cate.getSort(), cate.getImg(), cate.getType());
		}else {
			shopCateService.updateShopCate(cate);
		}
		return CommonResult.success(null);
	}

//	@ApiOperation(value = "编辑社区笔记")
//	@RequestMapping(value = "/update", method = RequestMethod.POST)
//	public CommonResult<Object> updateNote(@RequestBody @Validated CommunityNoteSaveRequest request) {
//		communityFrontService.updateNote(request);
//		return CommonResult.success();
//	}



	@OperationLog
	@ApiOperation(value = "编辑服务店铺类目")
	@ResponseBody
	@RequestMapping(value="/editServiceSubmit", method=RequestMethod.POST)
	public CommonResult<String> editServiceSubmit(UmsShopCate cate) throws ServiceException {
		if(cate.getId() == null){
			shopCateService.addShopCate(getCurrentShop(), cate.getPid(), cate.getName(), cate.getSort(), cate.getImg(),2);
		}else {
			shopCateService.updateShopCate(cate);
		}
		return CommonResult.success(null);
	}




	@ApiOperation(value = "删除")
	@OperationLog
	@ResponseBody
	@RequestMapping(value = {"/del"}, method = RequestMethod.POST)
	public CommonResult<String> del(@RequestBody Map<String, String> json) throws CommonException, ServiceException {
		String ids = json.get("ids");
		System.out.println("ids{}"+ids);
		if(StringUtils.isEmpty(ids)) {
			return CommonResult.failed( "请至少选择一条数据");
		}
		for(String id : ids.split(CommonConstant.DH_REGEX)){
			System.out.println("id{}"+id);
			shopCateService.deleteShopCate(Integer.valueOf(id));
		}
		return CommonResult.success(null);
	}


}
